package com.zhf.client;

import com.zhf.model.*;

import java.io.IOException;

public class ClientUserVIew {
    ClientTrainUI clientTrainUI = new ClientTrainUI();
    ClientExamUI clientExamUI = new ClientExamUI();
    private ClientInitClose cic;
    private User currentUser;
    public ClientUserVIew(){

    }

    public ClientUserVIew(ClientInitClose cic, User currentUser){
        this.cic = cic;
        this.currentUser = currentUser;
    }

    public void userrMainView(){
        System.out.println("\n------------请选择你要进行的操作------------");
        System.out.println("1.用户管理");
        System.out.println("2.考试管理");
        System.out.println("3.退出");
    }

    public void TraineeUserView(){
        System.out.println("****************************");
        System.out.println("1. 修改密码");
        System.out.println("2. 返回上一级菜单");
    }

    public void ExamQuestionsView() {
        System.out.println("****************************");
        System.out.println("1. 开始考试");
        System.out.println("2. 查询成绩");
        System.out.println("3. 导出成绩");
        System.out.println("4. 返回上一级菜单");
    }

    public void userMainOperate() throws IOException, ClassNotFoundException, InterruptedException {
        while (true){
            userrMainView();
            int choose1 = ClientScanner.getScanner().nextInt();
            switch (choose1){
                case 1:
                    clientTrainUI:while (true){
                        TraineeUserView();
                        int choose2 = ClientScanner.getScanner().nextInt();
                        switch (choose2){
                            case 1:
                                UserPassword userPassword = clientTrainUI.updatePassword();
                                userPassword.setUserName(currentUser.getUserName());
                                cic.getOos().writeObject(ApiRoute.UPDATE_PASSWORD);
                                cic.getOos().writeObject(userPassword);
                                System.out.println("客户端发送修改密码信息成功！");
                                UserPasswordMessage userPasswordMessage = (UserPasswordMessage) cic.getOis().readObject();
                                if("success".equals(userPasswordMessage.getMessage())){
                                    System.out.println("修改密码成功！");
                                }
                                else{
                                    System.out.println("修改密码失败！");
                                }
                                break;
                            case 2:
                                break clientTrainUI;
                            default:
                                System.out.println("输入错误，请重新选择！");
                        }
                    }
                    break;
                case 2:
                    clientExamUI: while (true){
                        ExamQuestionsView();
                        int choose2 = ClientScanner.getScanner().nextInt();
                        switch (choose2){
                            case 1:
                                System.out.println("请输入这次考试名称(以便记录查询)：");
                                String examName = ClientScanner.getScanner().next();
                                cic.getOos().writeObject(ApiRoute.START_EXAMINATION);
                                ExamUser examUser = new ExamUser(examName, null, this.currentUser);
                                cic.getOos().writeObject(examUser);
                                System.out.println("客户端发送开始考试信息成功！");
                                ExamUserMessage examUserMessage = (ExamUserMessage) cic.getOis().readObject();
                                if("success".equals(examUserMessage.getMessage())){
                                    ExamUser examUser1 = clientExamUI.startExam(examUserMessage);
                                    cic.getOos().writeObject(ApiRoute.STATISTIC_EXAM_RESULT);
                                    cic.getOos().writeObject(examUser1);
                                    System.out.println("客户端发送提交考卷信息成功！");
                                    ExamUserMessage examUserMessage1 = (ExamUserMessage) cic.getOis().readObject();
                                    if("success".equals(examUserMessage1.getMessage())){
                                        System.out.println("本次成绩的分数是：" + examUserMessage1.getExamUser().getGrade());
                                    }
                                    else{
                                        System.out.println("服务器端统计分数失败！");
                                    }
                                }else {
                                    if("notEnoughFail".equals(examUserMessage.getMessage())){
                                        System.out.println("题库不足，无法开始考试！");
                                    }
                                    else if("examNameExit".equals(examUserMessage.getMessage())){
                                        System.out.println("考试名称已经存在，请更换别的考试名称后再开始考试！");
                                    }else{
                                        System.out.println("服务器反馈本次无法进行考试！");

                                    }
                                }
                                break;
                            case 2:
                                System.out.println("请输入需要查询的考试名称：");
                                String examName1 = ClientScanner.getScanner().next();
                                ExamUser examUser1 = new ExamUser();
                                examUser1.setUser(this.currentUser);
                                examUser1.setExamName(examName1);
                                cic.getOos().writeObject(ApiRoute.QUERY_EXAM_GRADE);
                                cic.getOos().writeObject(examUser1);
                                System.out.println("客户端发送查询考试信息成功！");
                                ExamUserMessage examUserMessage2 = (ExamUserMessage) cic.getOis().readObject();
                                if("success".equals(examUserMessage2.getMessage())){
                                    System.out.println("该次考试成绩为 ：" + examUserMessage2.getExamUser().getGrade());
                                }
                                else{
                                    System.out.println("查找 " + examName1 + " 考试信息成功！");
                                }
                                break;
                            case 3:
                                System.out.println("请输入要导出的文件名称：");
                                String fileName = ClientScanner.getScanner().next();
                                cic.getOos().writeObject(ApiRoute.EXPORT_EXAM_GRADE);
                                cic.getOos().writeObject(this.currentUser);
                                ExamUserListMessage examUserListMessage = (ExamUserListMessage) cic.getOis().readObject();
                                if("success".equals(examUserListMessage.getMessage())){
                                    ClientExamUI.exportExamUserList(examUserListMessage, fileName);
                                }
                                else{
                                    System.out.println("未找到考试信息，请确认是否在系统中有过考试记录！");
                                }
                                break;
                            case 4:
                                break clientExamUI;
                            default:
                                System.out.println("输入错误，请重新选择！");
                        }
                    }
                    break;
                case 3:
//                    cic.clientClose();
                    cic.getOos().writeObject(ApiRoute.EXIT_TRAINEE);
                    return;
                default:
                    System.out.println("输入错误，请重新选择！");
            }
        }
    }

}
